package com.lee.lease.web.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lee.lease.common.custom.exception.LeaseException;
import com.lee.lease.common.result.ResultCodeEnum;
import com.lee.lease.common.util.JwtUtil;
import com.lee.lease.model.entity.SystemUser;
import com.lee.lease.model.enums.BaseStatus;
import com.lee.lease.web.admin.mapper.SystemUserMapper;
import com.lee.lease.web.admin.service.LoginService;
import com.lee.lease.web.admin.vo.login.CaptchaVo;
import com.lee.lease.web.admin.vo.login.LoginVo;
import com.lee.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SystemUserMapper mapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CaptchaVo getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        specCaptcha.setCharType(SpecCaptcha.TYPE_DEFAULT);
        //图片
        String verCode = specCaptcha.text().toLowerCase();
        //图片对应的key
        String key = "admin:login" + UUID.randomUUID().toString();
        // 存入redis并设置过期时间为60s
        redisTemplate.opsForValue().set(key,verCode,60,TimeUnit.SECONDS);
        //将图片和可以返回给客户端
        return new CaptchaVo(specCaptcha.toBase64(),key);
    }

    @Override
    public String login(LoginVo loginVo) {
        //判断收否输入了验证码
        if (!StringUtils.hasText(loginVo.getCaptchaCode())) {
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        //判断验证码是否正确
        String code = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if (!loginVo.getCaptchaCode().toLowerCase().equals(code)) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        //判断是否出入了用户名
        if (!StringUtils.hasText(loginVo.getUsername())) {
            throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_IS_EMPTY);
        }

        //判断用户是否存在
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername, loginVo.getUsername());
        SystemUser systemUser = mapper.selectOne(queryWrapper);
        //判断用户名是否存在
        if (systemUser==null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //判断用户状态是否禁用
        if (systemUser.getStatus().equals(BaseStatus.DISABLE)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //判断用户密码是否正确
        if(systemUser.getPassword().equals(DigestUtils.sha256Hex(loginVo.getPassword()))){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }


        return JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());
    }

    @Override
    public SystemUserInfoVo findByUserId(Long userId) {
        return mapper.findByUserId(userId);
    }
}
